package Adaptor;

import StaticClass.ArrSocket;
import java.awt.Component;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;

import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.swing.filechooser.FileSystemView;

public class JLocationListValue extends javax.swing.JList {

	private static final long serialVersionUID = 1L;

	public static final int FILETYPE_FOLDER = 0;
	public static final int FILETYPE_FILE = 1;
	public static final int FILETYPE_FOLDER_AND_FILE = 2;

	private JLocationListValue me = null;
	private JLocationListValue forFiles = null;
	private boolean showHidden = false;
	private int type = 2;
	private String parentFile = null;
	private String[] filenameExtensionFilter = null;

	public boolean isShowHidden() {
		return showHidden;
	}

	public void setShowHidden(boolean showHidden) {
		this.showHidden = showHidden;
	}

	public String[] getFilenameExtensionFilter() {
		return filenameExtensionFilter;
	}

	public void setFilenameExtensionFilter(String[] filters) {
		this.filenameExtensionFilter = filters;
	}

	public String getParentFile() {
		return parentFile;
	}

	public void setParentFile(String parentFile) {
		File f = new File(parentFile);
		this.parentFile = f.getPath();
		try {
			this.parentFile = f.getCanonicalPath();
		} catch (IOException e) {
			e.printStackTrace();
		}
		populateLists(new File(this.parentFile));
	}

	public String[] getSelectedFiles() {
		String[] files = null;

		if(me.getSelectedIndex()!=-1){
			Object[] data = me.getSelectedValues();
			if(data!=null){
				files = new String[data.length];
				for (int i = 0; i < data.length; i++) {
					String parent = getParentFile();
					parent = (!parent.endsWith(System.getProperty("file.separator")))? parent + System.getProperty("file.separator") : parent;
					files[i] = parent + data[i];
				}
			}
		}

		return files;
	}

	public String getSelectedFile() {
		String file = null;

		if(me.getSelectedIndex()!=-1){
			String parent = getParentFile();
			parent = (!parent.endsWith(System.getProperty("file.separator")))? parent + System.getProperty("file.separator") : parent;
			file = parent + me.getSelectedValue().toString();
		}

		return file;
	}

	public JLocationListValue(int type, boolean showHidden) {
		super();
		this.showHidden = showHidden;
		this.filenameExtensionFilter = null;
		this.type = type;
		this.forFiles = null;
		initGUI();
	}

	public JLocationListValue(int type, boolean showHidden, JLocationListValue forFiles, String[] filenameExtensionFilter) {
		super();
		this.showHidden = showHidden;
		this.filenameExtensionFilter = filenameExtensionFilter;
		this.type = type;
		this.forFiles = forFiles;
		initGUI();
	}

	public JLocationListValue(int type, boolean showHidden, String[] filenameExtensionFilter) {
		super();
		this.showHidden = showHidden;
		this.filenameExtensionFilter = filenameExtensionFilter;
		this.type = type;
		this.forFiles = null;
		initGUI();
	}
	
	public JLocationListValue(int type, JLocationListValue forFiles, boolean showHidden) {
		super();
		this.showHidden = showHidden;
		this.filenameExtensionFilter = null;
		this.type = type;
		this.forFiles = forFiles;
		initGUI();
	}

	private void initGUI() {
		me = this;
		try {
			setCellRenderer(new ComplexCellRenderer());
			this.addMouseListener(new MouseAdapter() {
				public void mouseClicked(MouseEvent evt) {

					String selected = (me.getSelectedValue()!= null)? me.getSelectedValue().toString():"";
					String parent = getParentFile();
					parent = (!parent.endsWith(System.getProperty("file.separator")))? parent + System.getProperty("file.separator") : parent;
					String str = parent+selected;
					if(evt.getClickCount()==2){
						File file = new File(str);
						if(file.isDirectory())
							setParentFile(str);
					}else if(evt.getClickCount()==1 && selected.equals(".."+System.getProperty("file.separator"))){
						File file = new File(str);
						if(file.isDirectory())
							setParentFile(str);
					}
				}
			});
			setSize(155, 240);
			this.addKeyListener(new KeyAdapter() {
				public void keyReleased(KeyEvent evt) {

				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
    
	protected void populateLists(File file ) {
		File files[] = file.listFiles();
		DefaultListModel lm = new DefaultListModel();
		if(file.getParent()!=null){
			if(this.type != JLocationListValue.FILETYPE_FILE)
				lm.addElement(".."+System.getProperty("file.separator"));
		}

		if(files!=null){
			for (int i = 0; i < files.length; i++) {
				File f = files[i];
				if(filenameExtensionFilter!=null){
					for (int j = 0; j < filenameExtensionFilter.length; j++) {
						if(f.canRead() && f.getName().endsWith(filenameExtensionFilter[j])){
							switch(this.type){
							case FILETYPE_FILE:
								if(f.isFile()){
									if(f.isHidden()){
										if(showHidden){
											lm.addElement(f.getName());
										}
									}else{
										lm.addElement(f.getName());
									}
								}
								break;
							case FILETYPE_FOLDER:
								if(f.isDirectory()){
									if(f.isHidden()){
										if(showHidden){
											lm.addElement(f.getName());
										}
									}else{
										lm.addElement(f.getName());
									}
								}
								break;
							default:
								if(f.isHidden()){
									if(showHidden){
										lm.addElement(f.getName());
									}
								}else{
									lm.addElement(f.getName());
								}
							}
							break;
						}
					}
				}
				else{
					switch(this.type){
					case FILETYPE_FILE:
						if(f.isFile()){
							if(f.isHidden()){
								if(showHidden){
									lm.addElement(f.getName());
								}
							}else{
								lm.addElement(f.getName());
							}
						}
						break;
					case FILETYPE_FOLDER:
						if(f.isDirectory()){
							if(f.isHidden()){
								if(showHidden){
									lm.addElement(f.getName());
								}
							}else{
								lm.addElement(f.getName());
							}
						}
						break;
					default:
						if(f.isHidden()){
							if(showHidden){
								lm.addElement(f.getName());
							}
						}else{
							lm.addElement(f.getName());
						}
					}
				}
			}
		}

		me.setModel(lm);
		if(forFiles!=null) if(file.isDirectory())forFiles.setParentFile(file.getPath());
	}


	class ComplexCellRenderer implements ListCellRenderer,Serializable {

		protected DefaultListCellRenderer defaultRenderer = new DefaultListCellRenderer();

            @Override
		public Component getListCellRendererComponent(JList list, Object value, int index,
				boolean isSelected, boolean cellHasFocus) {
                                
			String theText = (value!=null)?value.toString():"";
			String parent = getParentFile();
			parent = (!parent.endsWith(System.getProperty("file.separator")))? parent + System.getProperty("file.separator") : parent;
			String str = parent+theText;
			File f = new File(str);

			FileSystemView view = FileSystemView.getFileSystemView(); 
			Icon theIcon = view.getSystemIcon(f);
                        ArrSocket.icon.add(theIcon);
			JLabel renderer = (JLabel) defaultRenderer.getListCellRendererComponent(list, value, index,
					isSelected, cellHasFocus);

			if (theIcon != null) {
				renderer.setIcon(theIcon);
			}
			renderer.setText(theText);
			return renderer;
		}
	}
}
